package com.easygo.controller;

import com.easygo.service.SpecificationOptionService;
import com.easygo.service.SpecificationService;
import com.easygo.utils.PageUtils;
import com.xuexi.easygo.pojo.Specification;
import com.xuexi.easygo.pojo.SpecificationOption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Scope("prototype")
public class SpecificationController {

    @Autowired
    SpecificationService specificationService;

    @Autowired
    SpecificationOptionService specificationOptionService;

    @RequestMapping("specification_pages")
    public PageUtils<Specification> specification_pages(
            @RequestParam(defaultValue = "1",required = false,value = "pageIndex")Integer pageIndex,
            @RequestParam(defaultValue = "5",required = false,value = "pageSize")Integer pageSize,
            @RequestParam(defaultValue = "",required = false,value = "spec_name")String spec_name){
        Map<String,Object> map = new HashMap<>();
        map.put("pageStart",(pageIndex-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("spec_name","%"+spec_name+"%");
        System.out.println("条件是"+map);
        Integer totalConunt = specificationService.getTotalConunt(map);
        List<Specification> specificationPage = specificationService.getSpecificationPage(map);
        PageUtils<Specification> pageUtils = new PageUtils<>(pageSize,pageIndex,totalConunt,specificationPage);
        System.out.println("数据："+pageUtils);
        return pageUtils;
    }

    @RequestMapping("specification_add")
    public Integer specification_add(@RequestParam(value = "spec_name") String spec_name,
                                     @RequestParam(value = "option_name")  String[] option_name,
                                     @RequestParam(value = "orders")  Integer[] orders){

        //事务开启是在业务逻辑层
        Integer count = specificationService.addSpecificationTransaction(spec_name, option_name, orders);
        return count;
    }

    @RequestMapping("specification_getById")
    public Map<String,Object> getSpecification(@RequestParam(value = "id") Integer id){
        //规格对象
        Specification specification = specificationService.getSpecificationById(id);
        //规格对象下的选项
        List<SpecificationOption> optionsBySpecificationId = specificationOptionService.getSpecificationOptionsBySpecificationId(specification.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("specification", specification);
        map.put("specificationOptions", optionsBySpecificationId);
        return map;
    }

    @RequestMapping("/specification_update")
    public Integer specification_update(@RequestParam(value = "id") Integer id,
                                        @RequestParam(value = "spec_name") String spec_name,
                                        @RequestParam(value = "option_name") String[] option_name,
                                        @RequestParam(value = "orders") Integer[] orders) {
        return  specificationService.specification_update(id, spec_name, option_name, orders);
    }
}
